package com.apes.autoparts.supplier.longsky;

import com.apes.framework.jpa.repository.CustomRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Map;

public interface LongSkyRepository extends CustomRepository<LongSky, String> {


    int deleteAllBySupplierPlatformItemId(String id);

    @Query(value = " SELECT A.ID, A.CP, A.CYID," +
            "A.DC, A.DEPOTNO, A.FA, A.KCAMOUNT, A.NA1, A.NNO, A.NOTE, A.OPRC, A.POSI, A.TY, A.UNIT" +
            "   FROM long_sky A " +
            "  WHERE A.CYID = :source", nativeQuery = true)
    List<Map> getRowData(@Param("source") String source);

    @Query(value = "SELECT T.* FROM long_sky T where T.CYID in (:ids) and T.supplier_platform_item_id = :supplierPlatformItemId",nativeQuery = true)
    List<Map> findByIdsAndSupplierPlatformItem(@Param("ids") List<String> ids, @Param("supplierPlatformItemId") String supplierPlatformItemId);

    @Modifying
    @Query(value = "DELETE FROM long_sky T where T.supplier_platform_item_id = :supplierPlatformItemId and t.last_updated_stamp  > sysdate - (:hours / 24)",nativeQuery = true)
    int deleteBySupplierPlatformItemAndNotUpdate(@Param("supplierPlatformItemId") String supplierPlatformItemId, long hours);

    @Query(value = "select T.* FROM long_sky T where T.supplier_platform_item_id = :supplierPlatformItemId and t.last_updated_stamp  > sysdate - (:hours / 24)",nativeQuery = true)
    List<Map> findBySupplierPlatformItemAndNotUpdate(@Param("supplierPlatformItemId") String supplierPlatformItemId, long hours);

}
